# -*- coding = utf-8 -*-
# @Time : 2023/12/22 16:27
# @Author : 龙王赘婿冷帝彪少
# @File : logging_01.py
# @Software : PyCharm
# @Description : python 日志

import logging
import time
from logging import handlers


class LOG:
    __doc__ = "https://blog.csdn.net/Runner1st/article/details/96481954"
    __doc__ += "https://zhuanlan.zhihu.com/p/386379860"

    '''
        %(name)s：Logger的名字
        %(levelno)s：打印日志级别的数值
        %(levelname)s：打印日志级别的名称
        %(pathname)s：打印当前执行程序的路径，其实就是sys.argv[0]
        %(filename)s：打印当前执行程序名
        %(funcName)s：打印日志的当前函数
        %(lineno)d：打印日志的当前行号
        %(asctime)s：打印日志的时间
        %(thread)d：打印线程ID
        %(threadName)s：打印线程名称
        %(process)d：打印进程ID
        %(message)s：打印日志信息
        
        filemode
        a 追加到文件后
        w 覆盖之前内容
    '''

    # 简单demo
    @staticmethod
    def func_01():
        logging.debug("debug 调试信息")
        logging.info("info 正常操作")
        logging.warning("waring 告警")
        logging.error("error 错误信息")
        logging.critical("critical 致命错误")

    # 等级设置
    @staticmethod
    def func_02():
        logging.basicConfig(level=logging.DEBUG)
        logging.debug("debug 调试信息")
        logging.info("info 正常操作")
        logging.warning("waring 告警")
        logging.error("error 错误信息")
        logging.critical("critical 致命错误")

    # 更多信息
    @staticmethod
    def func_03():
        logging.basicConfig(level=logging.DEBUG,
                            format="%(asctime)s %(filename)s %(lineno)d %(levelname)s %(message)s")
        logging.debug("debug 调试信息")
        logging.info("info 正常操作")
        logging.warning("waring 告警")
        logging.error("error 错误信息")
        logging.critical("critical 致命错误")

    # 输出到文件
    @staticmethod
    def func_04():
        logging.basicConfig(level=logging.DEBUG,
                            format="%(asctime)s %(filename)s line:%(lineno)d [%(levelname)s]\t%(message)s",
                            filename="logging_01.log",
                            filemode="a")
        logging.debug("debug 调试信息")
        logging.info("info 正常操作")
        logging.warning("waring 告警")
        logging.error("error 错误信息")
        logging.critical("critical 致命错误")

    # 模块化操作
    @staticmethod
    def func_05():
        lo = logging.getLogger("mylog")

        my_format = logging.Formatter("%(asctime)s %(filename)s line:%(lineno)d [%(levelname)s]\t%(message)s")

        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(my_format)
        lo.addHandler(stream_handler)

        file_handler = logging.FileHandler("logging2.log")
        file_handler.setFormatter(my_format)
        lo.addHandler(file_handler)

        lo.setLevel(level=logging.DEBUG)

        lo.debug("debug 调试信息")
        lo.info("info 正常操作")
        lo.warning("waring 告警")
        lo.error("error 错误信息")
        lo.critical("critical 致命错误")

    # 时间操作
    @staticmethod
    def func_06():
        lo = logging.getLogger("mylog")

        my_format = logging.Formatter("%(asctime)s %(filename)s line:%(lineno)d [%(levelname)s]\t%(message)s")

        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(my_format)
        lo.addHandler(stream_handler)

        # D 天 S 秒
        file_handler = handlers.TimedRotatingFileHandler(filename="logging3.log", when='S')
        file_handler.setFormatter(my_format)
        lo.addHandler(file_handler)

        lo.setLevel(level=logging.DEBUG)

        while True:
            lo.debug("debug 调试信息")
            lo.info("info 正常操作")
            lo.warning("waring 告警")
            lo.error("error 错误信息")
            lo.critical("critical 致命错误")
            time.sleep(0.5)

    def __init__(self):
        # self.func_01()
        # self.func_02()
        # self.func_03()
        # self.func_04()
        # self.func_05()
        self.func_06()


if __name__ == "__main__":
    obj = LOG()
